﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Web.Security;

namespace PInitiatives.Models
{
    public partial class User
    {

        private PInitiativesEntities _db = new PInitiativesEntities();
        
        public IQueryable<Initiative> getParticipatedInitiatives()
        {
            IQueryable<Initiative> pi = _db.InitiativeUsers.Where(iu => iu.UserID == this.ID)
                .Join(_db.Initiatives, iu => iu.InitiativeID, i => i.ID, (iu, i) => i);
            return pi;
        }

        public IQueryable<Initiative> getParticipatedInitiatives(string initiativeUserStatus)
        {
            IQueryable<Initiative> pi = _db.InitiativeUsers.Where(iu => (iu.UserID == this.ID && iu.State.Equals(initiativeUserStatus)))
                .Join(_db.Initiatives, iu => iu.InitiativeID, i => i.ID, (iu, i) => i);
            return pi;
        }

        public bool Active
        {
            get
            {
                MembershipUser user = GetCurrentAspUser();
                if (user != null)
                    return user.IsApproved;
                else
                    return false;
            }
        }

        public string UserName
        {
            get
            {
                MembershipUser user = GetCurrentAspUser();
                if (user != null)
                    return user.UserName;
                else
                    return null;
            }
        }

        public string FullName
        {
            get 
            {
                return GetFullName();
            }
        }
        
        public string Email
        {
            get
            {
                MembershipUser user = GetCurrentAspUser();
                if (user != null)
                    return user.Email;
                else
                    return null;
            }
        }

        //public double UserRate
        //{
        //    get { return CountInitativeRate(); }
        //    //return CountInitativeRate();
        //}

        public User(string firstName, string secondName, string lastName, Guid aspUserID)
        {
            this.FirstName = firstName;
            this.SecondName = secondName;
            this.LastName = lastName;
            this.AspUserID = aspUserID;
        }

        private MembershipUser GetCurrentAspUser()
        {
            return Membership.GetUser(this.aspnet_Users.UserId);
        }

        private string GetFullName()
        {
            return this.FirstName + " " + this.LastName;
        }

        internal void Test()
        {
            Rate = 10;
        }
    }

    public class ManageUserModel
    {
        //ToDo: Walidacja
        public int ID { get; set; }

        [Display(Name="Imię")]
        public string FirstName { get; set; }

        [Display(Name = "Drugie imię")]
        public string SecondName { get; set; }

        [Display(Name = "Nazwisko")]
        public string LastName { get; set; }

        [Display(Name = "Nazwa użytkownika")]
        public string UserName { get; set; }

        [Display(Name = "Rola")]
        public string Role { get; set; }

        [Display(Name = "Aktywny")]
        public bool Active { get; set; }

        [Display(Name = "Integracja z Facebookiem")]
        public bool? Facebook { get; set; }

        [Display(Name = "Hasło")]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [Display(Name = "Powtórz hasło")]
        [DataType(DataType.Password)]
        [Compare("Password",ErrorMessage = "Hasła nie pasują do siebie")]
        public string ConfirmPassword { get; set; }

        public string Email { get; set; }

        public ManageUserModel() { }

        public ManageUserModel(int id, string firstName, string secondName, string lastName, bool? facebook, string email, string userName, string role, bool active)
        { 
            this.ID = id;
            this.FirstName = firstName;
            this.SecondName = secondName;
            this.LastName = lastName;
            this.Facebook = facebook;
            this.Email = email;
            this.UserName = userName;
            this.Role = role;
            this.Active = active;
        }

        public ManageUserModel(int id, string firstName, string secondName, string lastName, string email, bool? postOnFB)
        {
            this.ID = id;
            this.FirstName = firstName;
            this.SecondName = secondName;
            this.LastName = lastName;
            this.Facebook = postOnFB;
            this.Email = email;

        }
    }

    //public static class UserHelper
    //{

    //}
}